﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Web;

namespace Pos.SharedLibrary
{
    public class Print
    {
        public string dishName { get; set; }
        public string price { get; set; }
        public string printerName { get; set; }
        public string orderType { get; set; }
        public string subTotal = "SubTotal:";
        public string tax = "Tax:";
        public string total = "Total:";
        public string ticketID = "TicketID:";
        public string thankyou = "Thank you!";
        private int margins = 40;

        PrintDocument document = new PrintDocument();
        private int pageWidth = 302;
        private int pageHeight = 600;  

        public Print()  
        {　　　　   
            document.BeginPrint += new PrintEventHandler(document_BeginPrint);  
            document.PrintPage += new PrintPageEventHandler(document_PrintPage);  
            document.EndPrint += new PrintEventHandler(document_EndPrint);  
        }

        public Print(string dishname, string price, string printerName, string orderType)
        {
            this.dishName = dishname;
            this.price = price;
            this.printerName = printerName;
            this.orderType = orderType;
            document.BeginPrint += new PrintEventHandler(document_BeginPrint);
            document.PrintPage += new PrintPageEventHandler(document_PrintPage);
            document.EndPrint += new PrintEventHandler(document_EndPrint);  
        }

        private void document_EndPrint(object sender, PrintEventArgs e)  
        {  
              
        }  
 
        private void document_BeginPrint(object sender, PrintEventArgs e)  
        {  
 
        }  

        private void document_PrintPage(object sender, PrintPageEventArgs e)  
        {
            //title
            Font fontTitle = new Font("宋体", 12, FontStyle.Bold);
            e.Graphics.DrawString(GlobalManager.restaurantName, fontTitle, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(GlobalManager.restaurantName, fontTitle).Width / 2 + 1, 50);
            Font fontAddress = new Font("宋体", 8);
            e.Graphics.DrawString(GlobalManager.restaurantAddress, fontAddress, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(GlobalManager.restaurantAddress, fontAddress).Width / 2 + 1, 80);

            DateTime currentTime = new DateTime();
            currentTime = DateTime.Now;
            e.Graphics.DrawString(currentTime.ToString(), fontAddress, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(currentTime.ToString(), fontAddress).Width / 2 + 1, 100);

            e.Graphics.DrawString(orderType, fontAddress, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(orderType, fontTitle).Width / 2 + 1, 130);
            //end of title

            //Line  
            e.Graphics.DrawLine(new Pen(Brushes.Black, 3), margins / 2, 150, pageWidth - margins / 2, 150);


            //dishes Name  & price
            Font fontDish = new Font("宋体", 8);
            e.Graphics.DrawString(dishName, fontDish, Brushes.Black, margins, 158);
            e.Graphics.DrawString(price, fontDish, Brushes.Black,
           (float)pageWidth - margins - e.Graphics.MeasureString(price, fontDish).Width, 158);


            //total
            e.Graphics.DrawString(subTotal, fontTitle, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(subTotal, fontTitle).Width / 2 + 1, 280);
            e.Graphics.DrawString(tax, fontTitle, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(tax, fontTitle).Width / 2 + 1, 310);

            Font fontTotal = new Font("宋体", 15, FontStyle.Bold);
            e.Graphics.DrawString(total, fontTotal, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(total, fontTotal).Width / 2 + 1, 340);

            e.Graphics.DrawString(ticketID, fontTitle, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(ticketID, fontTitle).Width / 2 + 1, 380);

            //Line  
            e.Graphics.DrawLine(new Pen(Brushes.Black, 3), margins / 2, 410, pageWidth - margins / 2, 410);

            //Thankyou 
            e.Graphics.DrawString(thankyou, fontAddress, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(thankyou, fontAddress).Width / 2 + 1, 420);

        }

        public void PrintDocument()
        {
            //document.PrinterSettings.PrinterName = printerName;
            document.DefaultPageSettings.PaperSize = new PaperSize("Custum", pageWidth, pageHeight);
            PrintController printController = new StandardPrintController();
            document.PrintController = printController;
            //System.Windows.Forms.PrintPreviewDialog printPreview = new System.Windows.Forms.PrintPreviewDialog();
            //printPreview.Document = document;
            //printPreview.ShowDialog();  

            document.Print();
        }
    }
}
